<template>
  <div class="container">
    <div class="div_block">
      <div class="form_block">
        <el-form>
          <el-form-item>
            <el-input placeholder="用户名" v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input placeholder="登陆密码" v-model="form.password" type="password"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="btn_block">
        <el-button type="success" plain round @click="do_login" style="width: 100%;margin-bottom: 30px">登录</el-button>

        <div style="text-align: right">
          您还没有账号？ <a @click="to_register" style="color: lightblue">马上注册</a>
        </div>

      </div>
    </div>
  </div>
</template>

<script>

import axios from 'axios'

export default {
  name: "LoginBlock",
  data() {
    return {
      form: {
        username: "",
        password: ""
      },
      token: "TestToken",
    }
  },
  methods: {
    to_register() {
      this.$router.push('/sign/register');
    },
    do_login() {
      axios.get('http://localhost:8080/user/login/' + this.form.username + "/" + this.form.password)
        .then(data => {
          if (data.data.code == 1) { // 登录成功
            this.$router.push('/home'); // 页面跳转
            const login = data.data.data; // 登录的用户信息
            window.localStorage.setItem('user', JSON.stringify(login)); // 保存用户信息
          } else { // 登录失败
            this.$router.push('/'); 
            alert("登录失败！");
          }
        })
        .catch();
    }
  }
}
</script>

<style scoped>
.container {
  height: 100%;
  width: 100%;
}

.div_block {
  width: 80%;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.form_block {}

.btn_block {
  margin-top: 25px;
  color: #ffffff;
}
</style>
